<?php
class ModelBlogComment extends Model {
	
	public function addComment($post_id, $data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "blog_comments SET 
			author_name = '" . $this->db->escape($data['name']) . "', 
			author_email = '" . $this->db->escape($data['email']) . "', 
			author_ip = '" . $_SERVER['REMOTE_ADDR'] . "', 
			customer_id = '" . (int)$this->customer->getId() . "', 
			blog_post_id = '" . (int)$post_id . "', 
			content = '" . $this->db->escape($data['text']) . "', 
			created = NOW()");
	}
	
	
	public function getCommentsByPostId($post_id, $start = 0, $limit = 20) {
		$query = $this->db->query("SELECT 
										c.author_name, 
										c.author_email, 
										c.content, 
										c.created
									FROM 
										" . DB_PREFIX . "blog_comments c 
									WHERE 
										c.blog_post_id = '" . (int)$post_id . "' 
										AND c.approved = 1 
										AND c.published = 1
									ORDER BY 
										c.created DESC
									LIMIT " . (int)$start . "," . (int)$limit);
		
		return $query->rows;
	}	
	
	public function getTotalCommentsByPostId($post_id) {
		
		$query = $this->db->query("SELECT 
										COUNT(*) AS total
									FROM 
										" . DB_PREFIX . "blog_comments c 
									WHERE 
										c.blog_post_id = '" . (int)$post_id . "' 
										AND c.approved = 1 
										AND c.published = 1
									");
									
		return $query->row['total'];
	}
}
?>	